package com.chinasoft.mall.activity.adapter;

import java.util.List;

import android.app.Activity;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.model.response.GetGoodGrpImgByIdBean;
import com.chinasoft.mall.model.response.GoodsImgBean;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.widget.NetImageView;
import com.chinasoft.mall.widget.dialog.ShowImageDialog;

public class GallaryImageAdapter extends PagerAdapter {

	public class ViewInfo {
		public View view;
		public GoodsImgBean eb;
		public GetGoodGrpImgByIdBean egb;
	}

	public Activity activity;

	public GallaryImageAdapter(Activity activity, List<ViewInfo> mListViews) {
		this.activity = activity;
		this.mListViews = mListViews;
	}

	public List<ViewInfo> mListViews;

	public GallaryImageAdapter(List<ViewInfo> mListViews) {
		this.mListViews = mListViews;
	}

	public void setmListViews(List<ViewInfo> mListViews) {
		this.mListViews = mListViews;
		this.notifyDataSetChanged();
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView(mListViews.get(arg1).view);
	}

	@Override
	public void finishUpdate(View arg0) {
	}

	@Override
	public int getCount() {
		return mListViews.size();
	}

	@Override
	public Object instantiateItem(View arg0, int arg1) {
		View v = mListViews.get(arg1).view;
		NetImageView _niv = ((NetImageView) v.findViewById(R.id.netimage));
		final GoodsImgBean rb = mListViews.get(arg1).eb;
		final GetGoodGrpImgByIdBean grb = mListViews.get(arg1).egb;
		if(rb!=null){
			_niv.downloadbySize(
					StringUtil.grpDetailUrl(rb.getGOODS_ID(), rb.getIMG_URL()),
					650, 650, true, true);
			_niv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					// ((MainActivity)
					// SyActivity.this.getParent()).setCurrentActivity(
					// SpDetailActivity.class,_eb);
					ShowImageDialog sid=new ShowImageDialog(((MainActivity)activity.getParent()), StringUtil.grpDetailUrl(rb.getGOODS_ID(), rb.getIMG_URL()));
					sid.show();
				}
			});
		}else{
			_niv.downloadbySize(
					StringUtil.grpBreviaryUrl(grb.getGOODS_GRP_ID(), grb.getIMG_URL()),
					650, 650, true, true);
			_niv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					// ((MainActivity)
					// SyActivity.this.getParent()).setCurrentActivity(
					// SpDetailActivity.class,_eb);
					ShowImageDialog sid=new ShowImageDialog(((MainActivity)activity.getParent()), StringUtil.grpBreviaryUrl(grb.getGOODS_GRP_ID(), grb.getIMG_URL()));
					sid.show();
				}
			});
		}
		((ViewPager) arg0).addView(v, 0);
		return v;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == (arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
	}
	/*
	 * private LayoutInflater inflater; private ViewHolder _vh; private
	 * List<GoodsImgBean> rbs;
	 * 
	 * public GallaryImageAdapter(Activity activity,List<GoodsImgBean> rbs) {
	 * inflater = LayoutInflater.from(activity); this.rbs=rbs; }
	 * 
	 * public void setData(List<GoodsImgBean> rbs){ this.rbs=rbs;
	 * this.notifyDataSetChanged(); }
	 * 
	 * @Override public int getCount() { if (rbs == null) { return 0; } else {
	 * return rbs.size(); } }
	 * 
	 * @Override public Object getItem(int position) { if (rbs == null) { return
	 * null; } else { return rbs.get(position); } }
	 * 
	 * @Override public long getItemId(int position) { return position; }
	 * 
	 * @Override public View getView(int position, View convertView, ViewGroup
	 * parent) { if (convertView == null) { convertView =
	 * inflater.inflate(R.layout.spdetailgv, null); _vh = new ViewHolder();
	 * _vh.netimage = (NetImageView) convertView.findViewById(R.id.netimage);
	 * convertView.setTag(_vh); } else { _vh = (ViewHolder)
	 * convertView.getTag(); } GoodsImgBean rb=rbs.get(position);
	 * _vh.netimage.downloadbySize
	 * (StringUtil.grpDetailUrl(rb.getGOODS_ID(),rb.getIMG_URL
	 * ()),600,650,true,true); return convertView; }
	 * 
	 * class ViewHolder { public NetImageView netimage; }
	 */
}
